

/**
 * 上传文件
 */
let fetch = require('node-fetch');
let formData = require('form-data');

module.exports = async (options) => {
  let {
    imgPath
  } = options;
  let data = await getFileBufer(imgPath);
  if (data.err) {
    return null;
  }
  let form = new formData();
  form.append('xxx', xxx);
  form.append('pic', data.data);
  return fetch('http://xx.com/upload', {
    body: form,
    method: 'POST',
    headers: form.getHeaders()//要活的 form-data的头，否则无法上传
  }).then(res => {
    return res.json();
  }).then(data => {
    return data;
  })
}

/**
    * filepath = 相对根目录的路径即可
    */
async function getFileBufer(filePath) {

  return new Promise((resolve) => {
    fs.readFile(filePath, function (err, data) {
      var bufer = null;
      if (!err) {
        resolve({
          err: err,
          data: data
        });
      }

    });

  });
}
